package com.jmu.bishe.utils;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.Blob;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;

/**
 *<pre><b><font color="blue">StringUtils</font></b></pre>
 *<pre><b>&nbsp;--字符串处理公用类--</b></pre>
 * JDK版本：JDK1.5.0
 * @author  <b>李自立</b> 
 */
public class StringUtils {
	
	/**
	 * 判断字符串是否为空对象或是空串，是的话返回true
	 * @author 李自立
	 */
	public static boolean isBlank(String string){
		boolean bool = false;
		try {
			if((string == null) || (string.trim().length() == 0) || ("".equals(string))){
				bool = true;
			}
		} catch (Exception e) {
			System.out.println("判断字符串是否为空对象或是空串出错!");
			e.printStackTrace();
		}
		return bool;
	}
	
	
	/**
	 * XML字符串转换为XML文档，并设置编码为UTF-8
	 * @param strXML
	 * @return
	 * @author:李自立
	 */
	public static Document getDocumentByDocStr(String docStr){
		Document document = null;
		try {
			document = DocumentHelper.parseText(docStr);
			document.setXMLEncoding("UTF-8");
		} catch (DocumentException e) {
			System.out.println("XML字符串转换为XML文档出错!");
			e.printStackTrace();
		}
		return document;
	}
	
	/**
	 * 获得表单提交的数据
	 * @author 李自立
	 */
	public static String getRequestStr(String string){
		String str = string;
		try {
			if(!isBlank(string)){
				str = new String(string.getBytes("UTF-8"), "UTF-8");
			}
		} catch (Exception e) {
			System.out.println("编码转换出错");
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 表单提交方式，如果用Post的方式，则需要再进行一次的转编码
	 * @author 李自立
	 */
	public static String getPostRequestStr(String string){
		String str = string;
		try {
			if(!isBlank(string)){
				String sss = new String(string.getBytes("UTF-8"), "UTF-8");//ISO-8859-1
				str = URLDecoder.decode(sss, "UTF-8") ;
			}
		} catch (Exception e) {//UnsupportedEncodingException
			System.out.println("编码转换出错");
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 去掉字符串中的换行符
	 * @author 李自立
	 */
	public static String replaceStringLineRN(String string){
		String str = string;
		try {
			if(!isBlank(string)){
				str = string.replaceAll(System.getProperty("line.separator"), "");
			}
		} catch (Exception e) {
			System.out.println("字符串去掉换行符出错!");
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 用字符串中的换行符分隔字符串，一行为一条记录，添加到List中
	 * @author 李自立
	 */
	public static List<String> getStringArrayByStringLine(String string){
		List<String> list = null;
		try {
			if(!isBlank(string)){
				list = new ArrayList<String>();
				String[] str_array = string.split(System.getProperty("line.separator"));
				for(int i=0; i<str_array.length; i++){
					list.add(str_array[i]);
				}
			}
		} catch (Exception e) {
			System.out.println("分隔字符串，一行为一条记录，添加到List中出错!");
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * Object对象转换为字符串，如果对象为空，则返回空串，否则返回该对象转换的字符串
	 * @author 李自立
	 */
	public static String getObjectToString(Object object){
		String str = "";
		try {
			if(object != null){
				str = object.toString();
			}
		} catch (Exception e) {
			System.out.println("Object转换为字符串出错!");
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 对象转为Integer对象，如果对象为空，则转为空对象，否则返回该对象转为Boolean对象
	 * @author 李自立
	 */
	public static Boolean getObjectToBoolean(Object object){
		Boolean bool = null;
		try {			
			if(object != null){
				String str = getObjectToString(object);
				if(str.equalsIgnoreCase("TRUE")){
					bool = true;
				}else{
					bool = false;
				}
			}
		} catch (NumberFormatException e) {
			System.out.println("字符串TRUE、FALSE转为Boolean格式出错!");
		}
		return bool;
	}
	
	/**
	 * 对象转为Long对象，如果对象为空，则转为空对象，否则返回该对象转为Long数字格式
	 * @author 李自立
	 */
	public static Long getObjectToLong(Object object){
		Long num = null;
		try {			
			if(object != null){
				num = Long.valueOf(getObjectToString(object));
			}
		} catch (NumberFormatException e) {
			System.out.println("字符串转为Long数字格式出错!");
			e.printStackTrace();
		}
		return num;
	}
	
	/**
	 * 对象转为Integer对象，如果对象为空，则转为空对象，否则返回该 对象转为Integer数字格式的对象
	 * @author 李自立
	 */
	public static Integer getObjectToInteger(Object object){
		Integer num = null;
		try {			
			if(object != null){
				num = new Integer(getObjectToString(object));
			}
		} catch (NumberFormatException e) {
			System.out.println("字符串转为Integer数字格式出错!");
		}
		return num;
	}
	
	/**
	 * 对象转为Float对象，如果对象为空，则转为空对象，否则返回该对象转为Float数字格式的对象
	 * @author 李自立
	 */
	public static Float getObjectToFloat(Object object){
		Float f = null;
		try {			
			if(object != null){
				f = new Float(getObjectToString(object));
			}
		} catch (NumberFormatException e) {
			System.out.println("字符串转为Float数字格式出错!");
		}
		return f;
	}
	/**
	 * 对象转为Double对象，如果对象为空，则转为空对象，否则返回该对象转为Double数字格式的对象
	 * @author 李自立
	 */
	public static Double getObjectToDouble(Object object){
		Double d = null;
		try {			
			if(object != null){
				d = new Double(getObjectToString(object));
			}
		} catch (NumberFormatException e) {
			System.out.println("字符串转为Double数字格式出错!");
		}
		return d;
	}
	
	
	/**
	 * Boolean类型的对象转换为字符串，如果对象为空，则返回空串，否则返回该对象转换的字符串
	 * @author 李自立
	 */
	public static String getBooleanToString(Boolean bool){
		String str = "";
		try {
			if(bool != null){
				str = bool.toString();
			}			
		} catch (Exception e) {
			System.out.println("Boolean对象转为字符串出错!");
		}
		return str;
	}
	
	/**
	 * Long类型的对象转换为字符串，如果对象为空，则返回空串，否则返回该对象转换的字符串
	 * @author 李自立
	 */
	public static String getLongToString(Long n){
		String str = "";
		try {
			if(n != null){
				str = String.valueOf(n);
			}
		} catch (Exception e) {
			System.out.println("Long转换为字符串出错!");
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * Integer类型的对象转换为字符串，如果对象为空，则返回空串，否则返回该对象转换的字符串
	 * @author 李自立
	 */
	public static String getIntegerToString(Integer n){
		String str = "";
		try {
			if(n != null){
				str = String.valueOf(n);
			}
		} catch (Exception e) {
			System.out.println("Integer转换为字符串出错!");
		}
		return str;
	}
	
	/**
	 * Float类型的对象转换为字符串，如果对象为空，则返回空串，否则返回该对象转换的字符串
	 * @author 李自立
	 */
	public static String getFloatToString(Float num){
		String str = "";
		try {
			if(num != null){
				str = String.valueOf(num);
			}
		} catch (Exception e) {
			System.out.println("Float对象转换为字符串出错!");
		}
		return str;
	}
	
	/**
	 * 传入一个Double类型，转换为字符串，如果Double对象为空，则为空串
	 * @author 李自立
	 */
	public static String getDoubleToString(Double num){
		String str = "";
		try {
			if(num != null){
				str = String.valueOf(num);
			}
		} catch (Exception e) {
			System.out.println("Double对象转换为字符串出错!");
		}
		return str;
	}
	
	
	
	/**
	 * int类型的对象转换为字符串，如果对象为空，则返回空串，否则返回该对象转换的字符串
	 * @author 李自立
	 */
	public static String getIntToString(int n){
		String str = "";
		try {
			Integer num = new Integer(n);
			str = getIntegerToString(num);
		} catch (Exception e) {
			System.out.println("int类型数字转为字符串出错!");
		}
		return str;
	}

	/**
	 * 字符串转为int对象，如果字符串为空，则返回0，否则返回该字符串转int数字格式
	 * @author 李自立
	 */
	public static int getStringToInt(String string){
		int num = 0;
		try {			
			if(!isBlank(string)){
				num = Integer.parseInt(string);
			}
		} catch (NumberFormatException e) {
			System.out.println("字符串转为int类型数字格式出错!");
		}
		return num;
	}
	
	/**
	 * long类型的对象转换为字符串，如果对象为空，则返回空串，否则返回该对象转换的字符串
	 * @author 李自立
	 */
	public static String getlongToString(long n){
		String str = "";
		try {
			Long num = new Long(n);
			str = getlongToString(num);
		} catch (Exception e) {
			System.out.println("long类型数字转为字符串出错!");
		}
		return str;
	}

	/**
	 * 字符串转为long对象，如果字符串为空，则返回0，否则返回该字符串转int数字格式
	 * @author 李自立
	 */
	public static long getStringTolong(String string){
		long num = 0;
		try {			
			if(!isBlank(string)){
				num = Long.parseLong(string);
			}
		} catch (NumberFormatException e) {
			System.out.println("字符串转为long类型数字格式出错!");
		}
		return num;
	}
	
	/**
	 * Byte对象类型的值，转为String字符串类型，如果Byte对象为空，则为空串
	 * @author 李自立
	 */
	public static String getByteToString(Byte b){
		String str = "";
		try {
			if(b != null){
				str = String.valueOf(b);
			}
		} catch (Exception e) {
			System.out.println("Byte转换为字符串的时候出错!");
		}
		return str;
	}
	
	/**
	 * Oracle的Blob字段类型的值转为byte[]类型的值，如果bolb为空，则返回空对象
	 * @author 李自立
	 */
	public static byte[] getBlobToByte(Blob blob){
		byte[] b = null;
		try {
			if(blob != null){
				b = blob.getBytes(1, (int) blob.length());
			}
		} catch (Exception e) {
			System.out.println("Oracle的Blob类型转为byte[]类型出错!");
			e.printStackTrace();
		}
		return b;
	}
	
	/**
	 * 字符串类型的值，转为byte[]类型的值，如果字符串为空或空串，刚返回null
	 * @author 李自立
	 */
	public static byte[] getStringToByte(String string){
		byte[] b = null;
		try {
			if(!isBlank(string)){
				b = string.getBytes();
			}
		} catch (Exception e) {
			System.out.println("运行StrToBlob时出错!");
			e.printStackTrace();
		}
		return b;
	}
	
	/**
	 * 截取字符串最后一位
	 * @author 李自立
	 */
	public static String subStringLastWord(String string){
		String str = string;
		try {
			if(!isBlank(string)){
				str = string.substring(0, string.length()-1);
			}
		} catch (Exception e) {
			System.out.println("截到字符串最后一位出错!");
		}
		return str;
	}
	
	/**
	 * 文件名转编码
	 * @author 李自立
	 */
	public static String getFileNameByURLCode(String filename){
		String fn = filename;
		try {
			fn = URLEncoder.encode(filename,"UTF-8");
			if(fn.length() > 150){
				fn = new String(filename.getBytes("GBK"),"ISO-8859-1");
			}
		} catch (Exception e) {
			System.out.println("文件名转编码出错!");
			e.printStackTrace();
		}
		return fn;
	}
	
	/**
	 * 修补15位居民身份证号码为18位
	 * @param personIDCode 身份证
	 * @return 转换后的身份证
	 * @author:李自立
	 */
	public static String fixPersonIDCode15To18(String personIDCode) {
		String retIDCode = "";
		if (personIDCode == null || personIDCode.trim().length() != 15) {
			retIDCode = personIDCode;
		}
		else{
			String id17 = personIDCode.substring(0, 6) + "19" + personIDCode.substring(6, 15); // 15位身份证补'19'

			char[] code = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; // 11个
			int[] factor = { 0, 2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4, 8, 5, 10, 9, 7 }; // 18个;
			int[] idcd = new int[18];
			int i;
			int j;
			int sum;
			int remainder;

			for (i = 1; i < 18; i++) {
				j = 17 - i;
				idcd[i] = Integer.parseInt(id17.substring(j, j + 1));
			}

			sum = 0;
			for (i = 1; i < 18; i++) {
				sum = sum + idcd[i] * factor[i];
			}
			remainder = sum % 11;
			String lastCheckBit = String.valueOf(code[remainder]);
			retIDCode = id17 + lastCheckBit;
		}
		return retIDCode;
	}
	
	/**
	 * 18位身份证号码转换为15位居民身份证号码
	 * @param personIDCode 身份证
	 * @return 转换后的身份证
	 * @author:李自立
	 */
	public static String fixPersonIDCode18To15(String personIDCode) {
		String retIDCode = "";
		if (personIDCode == null || personIDCode.trim().length() != 18) {
			retIDCode = personIDCode;
		}
		else{
			retIDCode = personIDCode.substring(0, 6) + personIDCode.substring(8, 17);
		}
		return retIDCode;
	}
	
	/**
	 * 把列表的值转换成下拉框的选项
	 * @author:李自立
	 */
	public static String getSelectOptions(List list, String label, String value) {
		StringBuffer sb = new StringBuffer();
		for(int i=0; i<list.size(); i++){
			Map map = (Map) list.get(i);
			sb.append("<option value=\"" + map.get(value) + "\">" + map.get(label) + "</option>");
		}
		return sb.toString();
	}
	
	/**
	 * 清理List 将null转换成"",将其它类型的值转换成String
	 * @author 李自立
	 */
	public static List standardList(List list) {
		if(list != null){
			for(int i=0; i<list.size(); i++){
				Map row = (Map) list.get(i);
				Iterator it = row.keySet().iterator();
				while(it.hasNext()){
					String column = (String) it.next();
					String value = "";
					if(row.get(column) != null){
						value = row.get(column).toString();
					}
					row.put(column, value);
				}
			}
		}
		return list;
	}
	
	/**
	 * 把List中的Map中的key大写转换为小写
	 * @author 李自立
	 */
	public static List toLowerList(List list) {
		List resList = new ArrayList();
		try {
			if(list != null && list.size() >0){
				for(int i=0; i<list.size(); i++){
					Map row = (Map) list.get(i);
					Map newrow = new HashMap();
					if(row != null && row.keySet().size() >0){
						Iterator it = row.keySet().iterator();
						while(it.hasNext()){
							String key = it.next().toString();
							String value = "";
							if(row.get(key) != null){
								value = row.get(key).toString();
							}
							newrow.put(key.toLowerCase(), value);
						}
					}
					resList.add(newrow);
				}
			}
		} catch (Exception e) {
			System.out.println("把List中的Map中的key大写转换为小写出错!");
			e.printStackTrace();
		}
		return resList;
	}
	
	/**
	 * 把Map中的key大写转换为小写
	 * @author 李自立
	 */
	public static Map toLowerMap(Map map) {
		Map resMap = new HashMap();		
		try {
			if(map != null && map.keySet().size() >0){
				Iterator it = map.keySet().iterator();
				while(it.hasNext()){
					String key = it.next().toString();
					String value = "";
					if(map.get(key) != null){
						value = map.get(key).toString();
					}
					resMap.put(key.toLowerCase(), value);
				}
			}
		} catch (Exception e) {
			System.out.println("把Map中的key大写转换为小写!");
			e.printStackTrace();
		}
		return resMap;
	}
	public static String getStoreNum(String n)
	{
		if(StringUtils.isBlank(n))
		{
			return "";
		}
		long l = new Long(n);
		long g = 1024*1024;
		long m = 1024;
		java.text.DecimalFormat   df=new   java.text.DecimalFormat("#0.00"); 
		if(l>g)
		{
			double r = l*1.0/g;
			return df.format(r)+" G"; 
		}else if(l>m)
		{
			double r = l*1.0/m;
			return df.format(r)+" M";
		}else
		{
			return n+" K";
		}
		
	}

	/** 判断字段真实长度（中文2个字符，英文1个字符）
	 * @param value
	 * @return
	 * @author: ulyn
	 */
	public static int getStringRealLength(String value) {
		int valueLength = 0;
		String chinese = "[\u4e00-\u9fa5]";
		for (int i = 0; i < value.length(); i++) {
			String temp = value.substring(i, i + 1);
			if (temp.matches(chinese)) {
				valueLength += 2;
			} else {
				valueLength += 1;
			}
		}
		return valueLength;
	}

    /**
     * SQL语句转换成一句
     * @param sql
     * @param tjs
     * @return
     */
    public static String getRealSql(String sql,Object[] tjs) {
        if(tjs != null){
            for(Object param:tjs){
                if (param instanceof Integer) {
                    int value = ((Integer) param).intValue();
                    sql = sql.replace("?"," "+value+" ");
                } else if (param instanceof String) {
                    String value = (String) param;
                    sql = sql.replace("?","'"+value+"' ");
                }
            }
        }
        return sql;
    }
}
